package com.zf.shuashua.ui.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.aliyun.common.utils.DensityUtil;
import com.bumptech.glide.Glide;
import com.zf.shuashua.R;
import com.zf.shuashua.bean.BannerBean;
import com.zf.shuashua.utils.UIUtils;
import com.zhpan.bannerview.BaseBannerAdapter;
import com.zhpan.bannerview.BaseViewHolder;

public class MineBannerAdapter extends BaseBannerAdapter<BannerBean, BaseViewHolder<BannerBean>> {

    private int overrideHeight;

    private PicViewHolder.OnClickListener clickListener;

    public MineBannerAdapter(int height, PicViewHolder.OnClickListener listener) {
        overrideHeight = height;
        clickListener = listener;
    }

    @Override
    protected void onBind(BaseViewHolder<BannerBean> holder, BannerBean data, int position, int pageSize) {
        holder.bindData(data, position, pageSize);
    }

    @Override
    public BaseViewHolder<BannerBean> createViewHolder(View itemView, int viewType) {
        PicViewHolder viewHolder = new PicViewHolder(itemView, overrideHeight, clickListener);
        return viewHolder;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_user_banner;
    }

}


class PicViewHolder extends BaseViewHolder<BannerBean> {

    int height;

    private OnClickListener clickListener;

    public PicViewHolder(@NonNull View itemView, int height, OnClickListener listener) {
        super(itemView);
        this.height = height;
        clickListener = listener;
    }

    @Override
    public void bindData(BannerBean data, int position, int pageSize) {
        ImageView imageView = findView(R.id.banner_image);
        Glide.with(imageView)
                .load(data.banner_url)
                .override(UIUtils.getScreenWidthPx(imageView.getContext()) - DensityUtil.dip2px(imageView.getContext(), 24), height)
                .into(imageView);

        imageView.setOnClickListener(v -> {
            clickListener.onJump(data.jump_link, data.banner_name);
        });
    }

    public void setOnClickListener(OnClickListener listener) {
        clickListener = listener;
    }


    public interface OnClickListener {
        void onJump(String url, String title);
    }

}


